package com.lnking.lnkingplace.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lnking.common.http.ReturnResult;
import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.model.dto.cultureEnterprise.CultureEnterpriseExportDto;
import com.lnking.lnkingplace.model.dto.fourEnterprise.FourEnterpriseExportDto;
import com.lnking.lnkingplace.model.dto.outsiders.OutsidersDetailDto;
import com.lnking.lnkingplace.model.dto.outsiders.OutsidersDto;
import com.lnking.lnkingplace.model.dto.outsiders.OutsidersExportDto;
import com.lnking.lnkingplace.model.dto.user.UserDetailDto;
import com.lnking.lnkingplace.model.dto.user.UserDto;
import com.lnking.lnkingplace.model.entity.CultureEnterprise;
import com.lnking.lnkingplace.model.entity.FourEnterprise;
import com.lnking.lnkingplace.model.entity.Outsiders;
import com.lnking.lnkingplace.model.entity.User;
import com.lnking.lnkingplace.model.request.cultureEnterprise.CultureSearchRequest;
import com.lnking.lnkingplace.model.request.fourEnterprise.FourSearchRequest;
import com.lnking.lnkingplace.model.request.outsiders.OutsidersAddRequest;
import com.lnking.lnkingplace.model.request.outsiders.OutsidersEpidemicRequest;
import com.lnking.lnkingplace.model.request.outsiders.OutsidersSearchRequest;
import com.lnking.lnkingplace.model.request.outsiders.OutsidersUpdateRequest;
import com.lnking.lnkingplace.model.request.user.UserAddRequest;
import com.lnking.lnkingplace.model.request.user.UserSearchRequest;
import com.lnking.lnkingplace.model.request.user.UserUpdateRequest;
import com.lnking.lnkingplace.service.IOutsidersService;
import com.lnking.starter.export.utils.ExportUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 外来人员登记表 前端控制器
 * </p>
 *
 * @author sn
 * @since 2022-08-20
 */
@Api(tags = "外来人员")
@RestController
@RequestMapping("/outsiders")
public class OutsidersController {

    @Autowired
    IOutsidersService outsidersService;

    @Autowired
    UserUtil userUtil;
    @ApiOperation("添加")
    @PostMapping("add")
    public ReturnResult index(@RequestBody OutsidersAddRequest params){
        // 防止重复
        QueryWrapper<Outsiders> wapper = new QueryWrapper<>();
        wapper.lambda().eq(Outsiders::getPhone, params.getPhone());
        Integer integer = outsidersService.getBaseMapper().selectCount(wapper);
        Assert.isTrue(integer == 0, "手机号码不能重复");
        Outsiders outsiders = new Outsiders();
        BeanUtils.copyProperties(params, outsiders);
        // 获取上级的渠道
        UserDto user1 = userUtil.getUser();
        outsiders.setChannel(user1.getChannel());
        outsiders.setCreateTime(LocalDateTime.now());
        outsiders.setPlaceId(user1.getPlaceId());
        outsidersService.getBaseMapper().insert(outsiders);
        return  ReturnResult.success();
    }

    @ApiOperation("删除外来人员")
    @DeleteMapping("/{id}")
    public ReturnResult delete(@PathVariable("id") String id) {
        outsidersService.removeById(id);
        return ReturnResult.success();
    }

    @ApiOperation("修改外来人员信息")
    @PostMapping("/update")
    public ReturnResult update(@RequestBody @Valid OutsidersUpdateRequest params) {
        Outsiders outsiders = new Outsiders();
        BeanUtils.copyProperties(params, outsiders);
        outsidersService.updateById(outsiders);
        return ReturnResult.success();
    }

    @ApiOperation("获取外来人员详情")
    @GetMapping("/{id}")
    public ReturnResult getDetail(@PathVariable("id") String id) {
        OutsidersDetailDto dto = outsidersService.getOutsidersDetailById(id);
        return ReturnResult.success(dto);
    }



    @ApiOperation("查询外来人员列表")
    @GetMapping("/list")
    public ReturnResult list(OutsidersSearchRequest params) {
        UserDto user = userUtil.getUser();
        params.setChannel(user.getChannel());
        params.setPlaceId(user.getPlaceId());
        return ReturnResult.success(outsidersService.pageList(params));
    }

    @ApiOperation("重点人员统计")
    @GetMapping("/overview/list")
    public ReturnResult overview(OutsidersEpidemicRequest params) {
        return ReturnResult.success(outsidersService.overview(params));
    }


    @ApiOperation("导入")
    @PostMapping("import")
    private ReturnResult importExcel(@RequestParam("file") MultipartFile file) throws Exception{
        outsidersService.importExcel(file);
        return ReturnResult.success();
    }

    @ApiOperation(value = "导出")
    @GetMapping(value = "/export")
    public void exportInfo(OutsidersSearchRequest params, HttpServletResponse response) throws Exception {
        List<OutsidersDto> data = (List<OutsidersDto>) outsidersService.pageList(params);
        ExportUtils.export(data, response, OutsidersExportDto.class);
    }


}
